#!/bin/bash

# Get all function names for this application.
# Search for the function to ensure they are being used

cd /u/cem/cdc/web
filePrefix="$1"

NC='\033[0m' # No Color
Black='\033[0;30m'
DarkGray='\033[1;30m'
Blue='\033[0;34m'
LightBlue='\033[1;34m'
Green='\033[0;32m'
LightGreen='\033[1;32m'
Cyan='\033[0;36m'
LightCyan='\033[1;36m'
Red='\033[0;31m'
LightRed='\033[1;31m'
Purple='\033[0;35m'
LightPurple='\033[1;35m'
BrownOrange='\033[0;33m'
Yellow='\033[1;33m'
LightGray='\033[0;37m'
White='\033[1;37m'
clear && printf '\033[3J' #Clear the screen

function execute {
  patt="^[^  {}/*]"
  grep -rh --include="*$filePrefix*.css" --exclude-dir="free" "$patt" * |
    while read -r cssSelector;
  do
    firstChar=${cssSelector:0:1}
    if [ "$firstChar" == "." ] || [ "$firstChar" == "#" ]; then
      cssSelector=${cssSelector:1} #Remove '#' or '.'
    fi


    #Search for CSS selector excluding CSS files
    grep -rqn --include="*$filePrefix*" --exclude="*.swp" --exclude="*.css" \
      "$cssSelector" *
    if [ "$?" -eq 1 ]; then #If result != 1, No matches found
      if [[ "$count" -eq 0 ]]; then
        echo -e "${Red}***  CSS NOT USED       ***${NC}";
        count=$((count+1))
      fi
      echo "$cssSelector"
    fi
  done
}

function helper {
  echo "  aw_css [File Prefix]"
  echo "    [File Prefix] => Files to be included"
  echo "  eg aw_css acc_ "
}

if [[ "$filePrefix" == "?" ]] || [[ "$filePrefix" == *"help"* ]]
 then
  helper
elif [[ "$filePrefix" == "" ]]
 then
   echo -e "${Red}Missing parameter !!${NC}"
   helper
else
  execute $filePrefix
fi

